<?php
class Api{
    function get($req){
        $hash = $req->param;
        $row = pt\db\Mysql::init()->row("select *,id as `key` from mob_page where hash = ?",$hash);
        $row['items'] = json_decode($row['items']);
        return $row;
    }
    function post($req){
        $title = $req->body->title;
        $hash = get_hash_36($title.time());
        if(!$title)
            throw new ErrHttp("名称不能为空",500);
        $row = array(
            "title"=>$title,
            "add_time"=>date("Y-m-d H:i:s"),
            "hash"=>$hash,
            "status"=>0,
            "items"=>"[]"
        );
        pt\db\Mysql::init()->insert("mob_page",$row);
        return array("message"=>"新加成功");
    }
    function put($req){
        $hash = $req->param;
        $title = $req->body->title;
        if(!$title)
            throw new ErrHttp("名称不能为空",500);
        $row = array(
            "title"=>$title,
            "status"=>$req->body->status,
            "items"=>json_encode($req->body->items)
        );
        pt\db\Mysql::init()->update("mob_page",$row,array("hash"=>$hash));
        $res = array(
            "message"=>"修改成功"
        );
        return $res;
    }
    function delete($req){
        $hash = $req->param;
        pt\db\Mysql::init()->delete("mob_page",array("hash"=>$hash));
        $res = array(
            "message"=>"删除成功"
        );
        return $res;
    }
}
